<?php
namespace App\Models\Common\English;
use App\Facades\Site as CollAreaSite;;
use Illuminate\Database\Eloquent\Model;
use Cache,Validator;
use Symfony\Component\HttpKernel\Tests\DataCollector\DumpDataCollectorTest;

class EnCollArea extends Model{
    protected $table = 'coll_area';
    //设置主键值
    protected $primaryKey = 'id';
    public $timestamps = false;
    protected $fillable = [
        'cid', 'en_name','grade','pid','sort'
    ];

    /**
     * $parm  $id 当前选项ID
     * 获取当前选项地区自己ID
     * @return array
     */
    public static function CurrArea($id){
        $currArr = self::find($id)->toArray();
        $arr = array();
        if($currArr){
            if($currArr['grade'] == 1){
                $arr[] = $currArr['id'];
                //当前省地区
                $area = self::where('grade', 2)->where('pid', $currArr['cid'])->orderBy('cid','asc')->get();
                $area = setSort($area);
                foreach ($area as $k => $v) {
                    $arr[] = $v['id'];
                    //当前省地区县级市
                    $city = self::where('grade', 3)->where('pid', $v->cid)->orderBy('cid','asc')->get();
                    $city = setSort($city);
                    foreach($city as $vv){
                        $arr[] = $vv['id'];
                    }
                }
                array_push($arr,$currArr['id']);
            }else if($currArr['grade'] == 2){
                $arr = array_column( self::where('pid',$currArr['cid'])->get()->toArray(),'id');
                array_push($arr,$currArr['id']);
            }else{
                $arr[] = $currArr['id'];
            };
            return $arr;
        }else{
            return array();
        }
    }

    //--------------------------------------------------------------------------------------------------------------------------------

    /**
     * 提取省份
     * @return array
     */
    public static function getProvince(){
        $area=self::where('grade', 1)->get();
        $area = setSort($area);
        $areaArray=array();
        foreach($area as $key=>$value){
            $areaArray[]=array('value'=>$value['cid'],'text'=>$value['en_name']);
        }
        return $areaArray;
    }
    /**
     * 提取地级市
     * @return array
     * $pid:省份的id
     */
    public static function getArea($pid){
        $area=self::where('grade', 2)->where('pid',$pid)->get();
        $area = setSort($area);
        $areaArray=array();
        foreach($area as $key=>$value){
            $areaArray[]=array('value'=>$value['cid'],'text'=>$value['en_name']);
        }
        return $areaArray;
    }
    /**
     * 提取县市区
     * @return array
     * $pid:地级市id
     */
    public static function getCity($pid){
        $area=self::where('grade', 3)->where('pid',$pid)->get();
        $area = setSort($area);
        $areaArray=array();
        foreach($area as $key=>$value){
            $areaArray[]=array('value'=>$value['cid'],'text'=>$value['en_name']);
        }
        return $areaArray;
    }

    /**
     * 提取省和地区//二维
     * @return array
     */
    public static function getProvinceArea(){
        $provinceArray=array();
        if (Cache::has('areaProvinceArea_en')) {
            $provinceArray= Cache::get('areaProvinceArea_en');
        }else {
            $province = self::where('grade', 1)->get();
            $province = setSort($province);
            foreach ($province as $key => $value) {
                $area = self::where('grade', 2)->where('pid', $value['cid'])->get();
                $area = setSort($area);
                $areaArray = array();
                foreach ($area as $k => $v) {
                    $areaArray[] = array('value' => $v['en_name'], 'text' => $v['cid']);
                }
                $provinceArray[] = array('value' => $value['en_name'], 'text' => $value['cid'], 'area' => $areaArray);
            }
            Cache::forever('areaProvinceArea_en', $provinceArray);
        }
        return $provinceArray;
    }

    /**
     * 提取省和地区和县市区//三维
     * @return array
     */
    public static function getComplete(){
        $provinceArray=array();
        if (Cache::has('areaComplete_en')) {
            $provinceArray= Cache::get('areaComplete_en');
        }else{
            $province=self::where('grade', 1)->get();
            $province = setSort($province);
            foreach($province as $key=>$value){
                $area=self::where('grade', 2)->where('pid',$value['cid'])->get();
                $area = setSort($area);
                $areaArray=array();
                foreach($area as $k=>$v) {
                    $city=self::where('grade', 3)->where('pid',$v['cid'])->get();
                    $city = setSort($city);
                    $cityArray=array();
                    foreach($city as $city_k=>$city_v){
                        $cityArray[]=array('value'=>$v['en_name'],'text'=>$v['cid']);
                    }
                    $areaArray[]=array('value'=>$v['en_name'],'text'=>$v['cid'],'city'=>$cityArray);
                }
                $provinceArray[]=array('value'=>$value['en_name'],'text'=>$value['cid'],'area'=>$areaArray);
            }
            Cache::forever('areaComplete_en', $provinceArray);
        }
        return $provinceArray;
    }

    /**
     * 获取地区的省市县三级中文
     * $cid:选项编号
     * @return： 选项名称
     */
    public static function getAreaAddress($cid){
        if($cid == '') return '';
        $province = substr($cid,0,2) . '0000';
        $area = substr($cid,0,4) . '00';
        $province= self::getAreaName($province);
        $area= self::getAreaName($area);
        $city= self::getAreaName($cid);
        return $province .','. $area .','. $city;
    }

    /**
     * 获取地区的市县二级中文
     * $cid:选项编号
     * @return： 当前城市地区
     */
    public static function getCityAddress($cid){
        if($cid == '') return '';
        //$province = substr($cid,0,2) . '0000';
        $area = substr($cid,0,4) . '00';
       // $province= self::where('cid',$province)->first();
        $area= self::getAreaName($area);
        $city= self::getAreaName($cid);
        return $area .','. $city;
    }

    /**
     * 获取地区的中文
     * $cid:选项编号
     * @return： 选项名称
     */
    public static function getAreaName($cid){
        $city= self::where('cid',$cid)->first();
        if($city){
            return $city->en_name;
        }
        return'';
    }

    /**
     * 获取当前站点的城市
     */
    public static function getSiteCity(){
        return self::getCity(CollAreaSite::get('webareacode'));
    }


    /**
     * 清理本模型中的所有缓存数据
     */
    public static function clearCcache(){
        Cache::forget('areaComplete_en');
        Cache::forget('areaProvinceArea_en');
        return '';
    }
}